<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\TagExistsException;
use App\Exceptions\TagKnowledgeExistsException;
use App\Models\Knowledge;
use App\Models\Tag;

class TagController extends Controller
{
    /*
     * @api get /course-api/a/tags 标签列表
     * @version 0.0.1
     * @group admin
     *
     * @param query int [pageSize] 页数，默认15
     * @param query int [page] 页码，默认1
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function index()
    {
        $this->validate($this->request, [
            "pageSize" => "integer|max:100|min:5",
            "page" => "integer|min:1"
        ]);
        $page = intval($this->request->input("page", 1));
        $pageSize = intval($this->request->input("pageSize", 15));
        $query = Tag::query();
        $total = $query->count();
        $tagObjs = $query->orderBy('id', 'desc')->forPage($page, $pageSize)
            ->get();
        $items = [];
        if ($total) {
            /** @var Tag $tagObj */
            foreach ($tagObjs as $tagObj) {
                $items[] = [
                    'id' => $tagObj->getId(),
                    'cnName' => $tagObj->getCnName(),
                    'enName' => $tagObj->getEnName(),
                    'audio' => $tagObj->audio()->first(),
                    'cover' => $tagObj->cover()->first(),
                    'description' => $tagObj->getDescription(),
                ];
            }
        }
        return $this->sendSuccessJson(['total'=>$total, 'items'=>$items]);
    }

    /*
     * @api post /course-api/a/tags 新增标签
     * @version 0.0.1
     * @group admin
     *
     * @param body string cnName 中文名称
     * @param body string enName 英文名称
     * @param body string audioId 音频ID
     * @param body string imageId 封面图片ID
     * @param body string [description] 描述
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'cnName' => 'required|string',
            'enName' => 'required|string',
            'audioId' => 'required|integer',
            'imageId' => 'required|integer',
        ]);
        $cnName = $this->request->input('cnName');
        $enName = $this->request->input('enName');
        $audioId = $this->request->input('audioId');
        $imageId = $this->request->input('imageId');
        $description = $this->request->input('description', '');
        if (Tag::where('en_name', $cnName)->exists()) {
            throw new TagExistsException();
        }
        $tagObj = new Tag();
        $tagObj->setCnName($cnName);
        $tagObj->setEnName($enName);
        $tagObj->setAudio($audioId);
        $tagObj->setCover($imageId);
        $tagObj->setDescription($description);
        $tagObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api put /course-api/a/tags/{tagId} 更新标签
     * @version 0.0.1
     * @group admin
     *
     * @param body string cnName 中文名称
     * @param body string enName 英文名称
     * @param body string audioId 音频ID
     * @param body string imageId 封面图片ID
     * @param body string description 描述
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function update($id)
    {
        $this->request->merge(['id' => $id]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'cnName' => 'required|string',
            'enName' => 'required|string',
            'audioId' => 'required|integer',
            'imageId' => 'required|integer',
        ]);
        $cnName = $this->request->input('cnName');
        $enName = $this->request->input('enName');
        $audioId = $this->request->input('audioId');
        $imageId = $this->request->input('imageId');
        $description = $this->request->input('description', '');
        $existsId = Tag::where('en_name', $cnName)->value('id');
        if ($existsId && $existsId != $id) {
            throw new TagExistsException();
        }
        /** @var Tag $tagObj */
        $tagObj = Tag::find($id);
        $tagObj->setCnName($cnName);
        $tagObj->setEnName($enName);
        $tagObj->setAudio($audioId);
        $tagObj->setCover($imageId);
        $tagObj->setDescription($description);
        $tagObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api delete /course-api/a/tags/{tagId} 删除标签
     * @version 0.0.1
     * @group admin
     *
     * @param path int tagId 标签ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function destroy($id)
    {
        /** @var Tag $tagObj */
        $tagObj = Tag::find($id);
        if ($tagObj->knowledge()->exists()) {
            throw new TagKnowledgeExistsException();
        }
        $tagObj->forceDelete();
        return $this->sendSuccessJson();
    }
}
